﻿#include <iostream>
#include <vector>
#include <unordered_map>


using namespace std;


class Solution {
public:
	vector<int> twoSum(vector<int>& nums, int target) {
		unordered_map<int, int> hashtable;
		for (int i = 0; i < nums.size(); ++i) {
			auto it = hashtable.find(target - nums[i]);
			if (it != hashtable.end()) {
				cout << "i = " << i << " nums[i] = " << nums[i] << " target-nums[i] = " << endl;
				return { it->second,i };
			}
			hashtable[nums[i]] = i;
		}
		return {};
	}
};

int main() {
	Solution solution;
	vector<int> nums = { 2,7,11,15 };
	int target = 9;
	vector<int> result = solution.twoSum(nums, target);
	if (result.size() == 2) {
		cout << "找到两个数的索引为：" << result[0] << " 和 " << result[1] << endl;
		cout << "对应的元素相加验证：" << nums[result[0]] + nums[result[1]] << endl;
	}
	else {
		cout << "未找到符合要求的两数之和对应的索引" << endl;
	}
	return 0;
}
